package com.sdkx.app.ViewModel;

import com.sdkx.app.model.AirWay;
import com.sdkx.app.model.AirWaySegment;
import com.sdkx.app.model.WayPoint;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

/**
 * 航线管理业务类
 */
public class AirWayManager {
    /**
     * 获取所有航线数据
     * @return
     */
    public static List<AirWay> getItems(String name){
        if(name==null)
            return LitePal.findAll(AirWay.class);
        else
            return LitePal.where("name like ?", "%"+name+"%").find(AirWay.class);
    }

    /**
     * 获取指定类型的航线
     * @param type
     * @return
     */
    public static List<AirWay> getItemsByType(int type){
        return LitePal.where("type = ?",String.valueOf(type)).find(AirWay.class);
    }

    public static AirWay getItem(int id){
        List<AirWay> wayPoints = LitePal.where("id = ?", String.valueOf(id)).find(AirWay.class);
        if(wayPoints.size()>0)
            return wayPoints.get(0);
        else
            return null;
    }

    /**
     * 返回查找的航线
     * @param name
     * @return
     */
    public static AirWay getItem(String name) {
        List<AirWay> airWays = LitePal.where("name = ?", name).find(AirWay.class);
        if (airWays.size() > 0)
            return airWays.get(0);
        else
            return null;
    }

    /**
     * 新建一条航线,如果航线名称已经存在则会返回false
     * @param airWay
     * @return
     */
    public static boolean insert(AirWay airWay){
        if(airWay==null){
            return false;
        }
        //存储航线到航线表
        return airWay.save();
    }

    /**
     * 批量存储，大量数据存储时采用此方法速度较快
     * @param airWays
     */
    public static void insertList(List<AirWay> airWays){
        if(airWays==null)
            return;
        LitePal.saveAll(airWays);
    }

    /**
     * 判断一个名称是否已经使用
     * @param name
     * @return
     */
    public static boolean cotains(String name){
        return LitePal.where("name = ?",name).count(AirWay.class)>0;
    }

    /**
     * 删除一条航线
     *同时会自动删除航线和点的关系
     * @param name
     */
    public static void delete(String name) {
        List<AirWay> airWays = LitePal.where("name = ?", name).find(AirWay.class);
        if(airWays.size()>0){
            LitePal.deleteAll(AirWaySegment.class,"airWayId = ?",String.valueOf( airWays.get(0).getId()));
            airWays.get(0).delete();
        }
    }

    /**
     * 删除所有数据
     */
    public static void deleteAll(){
        LitePal.deleteAll(AirWay.class);
    }

    /**
     * 更新一条航线,此航线对象必须是已经持久化的对象
     * @param airWay 新航线对象
     */
    public static void update(AirWay airWay){
        if(airWay==null||!airWay.isSaved())
            return;
       airWay.update(airWay.getId());
    }

    /**
     * 获取航线中的点
     * @param name
     * @return
     */
    public static List<WayPoint> getWayPointsInAirWay(String name) {
        AirWay airWay = getItem(name);
        List<WayPoint> wayPoints = new ArrayList<>();
        if(airWay!=null) {
            List<AirWaySegment> airWaySegments = LitePal.select("waypointid").where("airwayid = ?", String.valueOf(airWay.getId())).order("no").find(AirWaySegment.class);
            for (AirWaySegment airWaySegment : airWaySegments) {
                wayPoints.add(LitePal.find(WayPoint.class, airWaySegment.getWayPointId()));
            }
        }
        return wayPoints;
    }

    /**
     * 更新航线上的点，如更改点的顺序，增加点，删除点
     * 需要注意的是，若修改航点信息都需要额外调用航点相关的管理类去更新数据库信息
     *
     * @param wayName
     * @param wayPoints
     */
    public static void updateWayPointsInAirWay(String wayName, List<WayPoint> wayPoints) {
        //TODO:此方法还有待优化，不用先删除再添加新的，而是删除多余的，更新现有的
        AirWay airWay = getItem(wayName);
        if (airWay == null)
            return;
        LitePal.deleteAll(AirWaySegment.class, "airwayid = ?", String.valueOf(airWay.getId()));
        int no = 1;
        for (WayPoint wayPoint : wayPoints) {
            List<WayPoint> wayPoints1 = LitePal.where("name = ?", wayPoint.getName()).find(WayPoint.class);
            WayPoint wayPoint1 = null;
            if (wayPoints1.size() == 0) {//航点不存在
                wayPoint.save();
                wayPoint1 = LitePal.where("name = ?", wayPoint.getName()).find(WayPoint.class).get(0);
            } else {
                wayPoint1 = wayPoints1.get(0);
            }
            AirWaySegment airWaySegment = new AirWaySegment();
            airWaySegment.setNo(no++);
            airWaySegment.setAirWayId(airWay.getId());
            airWaySegment.setWayPointId(wayPoint1.getId());
            airWaySegment.save();
        }
    }
}
